let app = getApp();
import Dialog from '../../../dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:null,
    type:null,
    orderType:null,
    refundDetails: {},
    orderDetails: {}, //订单详情
    receivingStatus:"not_received",
    refundTypes:[],
    reasons: [
      '买多了/买错了',
      '计划有变，暂时不需要了',
      '不能送外卖',
      '使用条件不清楚',
      '联系不上商家',
      '约不到合适的时间',
      '可用的门店距离太远',
      '其他平台/方式购买更优惠',
      '评价不好',
      '担心安全问题（疫情、天气等）'
    ],
    refundValue:'',
    refundRadio:'',
    explain: '',
    refundSum:1,
    refundShow:false,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.orderId??'') !== '') {
      this.setData({
        orderId: Number(options.orderId),
        type: options.type,
        orderType: options.orderType
      })
      // this.getMiniTemplateCorrespond();
      this.goodsRefund()
    }
    
  },
  onRefund() {  //提交售后订单
    if (app._isTextEmpty(this.data.refundValue)) {
      wx.showToast({
        icon: 'none',
        title: '请选择退款原因',
        duration: 1500
      });
      return;
    }
    Dialog.confirm({
      message: '退款申请一旦提交，将不能撤销，是否确认继续退款？',
    })
      .then(() => {
        let urlStr = app.getNetAddresss("refund.apply.store");
        let json = {
          content: this.data.explain,
          freight_price: 0,
          order_id: this.data.orderId,
          other_price: 0,
          part_refund: 1,
          reason: this.data.refundValue,
          receive_status: 0,
          refund_id: this.data.orderId,
          refund_type: 0,
          refund_way_type: 0,
        };
        app._postNetWork({
          url: urlStr,
          data:json,
          success:  (resdata) => {
            let res = resdata.data;
            if (res.result == 1) {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
              let pages = getCurrentPages();
              let prevPage = pages[pages.length - 2]; //上一个页面
              prevPage.setData({ //要传递的参数
                tabIndex: 4
              })
              if(this.data.orderType) {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 2
                  })                
                }, 1000);
              } else {
                  //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  })                
                }, 1000);
              }
             
             
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      })
      .catch(() => {
        // on cancel
      });
  },
  goodsRefund() { //选择售后详情
    let urlStr = app.getNetAddresss("refund.apply");
    let json = {
      order_id: this.data.orderId,
      type: this.data.type 
    };
    app._getNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            orderDetails: res.data.order,
            refundSum: res.data.order.order_goods[0].total
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onrefundChange(event) {
    this.setData({
      refundRadio: event.detail,
    })
  },
  onTotalChange(event) {
    this.setData({
      refundSum: event.detail
    })
  },
  explainInput(event) {
    this.setData({
      explain: event.detail.value
    })
  },
  refundBtn() {
    this.setData({
      refundValue: this.data.refundRadio,
      refundShow:false
    })
  },
  getMiniTemplateCorrespond() {
    let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
    let json = {
      type:this.data.type = 2
    };
    app._getNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onClose() {
    this.setData({
      refundShow:false
    })
  },
  onCause() {
    this.setData({
      refundShow:true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})